/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import { TabBar } from "antd-mobile"
import * as React from "react"
import { RouteComponentProps, withRouter } from "react-router"
import Cart from "../cart"
import { Categories } from "../categories"
import Home from "../home"
import Icon from "../icon"
import MyCenter from "../my-center"

interface FirstPageProps extends RouteComponentProps {

}

interface FirstPageState {
  selectedTab: string;
}

class FirstPage extends React.Component<FirstPageProps, FirstPageState> {

  public constructor(props: Readonly<any>) {
    super(props)
    this.state = {
      selectedTab: "/index",
    }
  }

  public componentDidMount(): void {
    const { match } = this.props
    const { selectedTab } = this.state
    if (match.path !== selectedTab) {
      this.setState({ selectedTab: match.path })
    }
  }

  public render() {
    const { history } = this.props
    const { selectedTab } = this.state
    return (
      <TabBar
        unselectedTintColor="#949494"
        tintColor="#dc221d"
        barTintColor="white"
        tabBarPosition="bottom">
        <TabBar.Item title="首页"
                     key="/"
                     icon={<Icon type="home"/>}
                     selectedIcon={<Icon type="home_fill"/>}
                     selected={selectedTab === "/"}
                     onPress={() => history.push("/")}>
          {selectedTab === "/" && <Home/>}
        </TabBar.Item>
        <TabBar.Item title="分类"
                     key="/categories"
                     icon={<Icon type="similar"/>}
                     selectedIcon={<Icon type="similar"/>}
                     selected={selectedTab === "/categories"}
                     onPress={() => history.push("/categories")}>
          {selectedTab === "/categories" && <Categories/>}
        </TabBar.Item>
        <TabBar.Item title="购物车"
                     key="/cart"
                     icon={<Icon type="cart"/>}
                     selectedIcon={<Icon type="cart_fill"/>}
                     selected={selectedTab === "/cart"}
                     onPress={() => history.push("/cart")}>
          {selectedTab === "/cart" && <Cart/>}
        </TabBar.Item>
        <TabBar.Item title=" 个人中心"
                     key="/my-center"
                     icon={<Icon type="my"/>}
                     selectedIcon={<Icon type="my_fill"/>}
                     selected={selectedTab === "/my-center"}
                     onPress={() => history.push("/my-center")}>
          {selectedTab === "/my-center" && <MyCenter/>}
        </TabBar.Item>
      </TabBar>
    )
  }
}

export default withRouter(FirstPage)
